package com.example.mybatisflexdemo.service.impl;

import com.example.mybatisflexdemo.convertor.UserConvertor;
import com.example.mybatisflexdemo.dto.UserAddDTO;
import com.example.mybatisflexdemo.dto.UserDTO;
import com.example.mybatisflexdemo.entity.User;
import com.example.mybatisflexdemo.mapper.UserMapper;
import com.example.mybatisflexdemo.service.UserService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final UserConvertor userConvertor;

    @Override
    public List<UserDTO> getUserList() {
        List<User> users = userMapper.selectListByQuery(QueryWrapper.create());
        return userConvertor.convertDoToDtoList(users);
    }

    @Transactional(rollbackFor = Exception.class)
    public int addUser(UserAddDTO user) {
        User newUser = userConvertor.convertAddDtoToDo(user, user.getAvatar());
        return userMapper.insert(newUser);
    }

    @Override
    public int delUser(Integer id) {
        return userMapper.deleteById(id);
    }
}


